﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;


using WinnerCMS.Common;
using System.Linq;
using WinnerCMS.Model;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_IPBlackList。
    /// </summary>
    public class IPBlackListDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(IPBlackList model)
        {
            using (Entities db = new Entities())
            {
                model.CreateTime = DateTime.Now;
                model.UpDateTime = DateTime.Now;
                db.IPBlackList.Add(model);
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(IPBlackList model)
        {

            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.IPBlackList where Ids.Contains(x.Id) select x;
                q.ToArray().ForEach(x => db.IPBlackList.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        /// <summary>
        /// 取得黑名单
        /// </summary>
        /// <returns></returns>
        public IList<IPBlackList> GetBlackList()
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.IPBlackList
                        select new { x.SectorStart, x.SectorEnd };

                return q.ToArray().Select(x => new IPBlackList() { SectorStart = x.SectorStart, SectorEnd = x.SectorEnd }).ToArray();
            }
        }
        #region 绑定分页
        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<IPBlackList> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.IPBlackList
                            select x;
                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToArray();
            }
        }
        #endregion

    }
}